﻿namespace Bootsy.PEG

class StringParser(IParser):
	
	[getter(Input)]
	_input as IMarkResetEnumerator
	
	[getter(Context)]
	_context
	
	def constructor(text as string):
		_input = StringMarkResetEnumerator(text)
		
	def constructor(text as string, context):
		self(text)
		_context = context
	
	def Evaluate(rule as IGrammarRule) as Action:
		mark = _input.Mark()
		try:
			SkipWhitespace(_input)
			action = rule.Apply(self)
		ensure:
			if action is null:
				_input.Reset(mark)
		return action
		
	def SkipWhitespace(input as IMarkResetEnumerator):
		mark = input.Mark()
		while true:
			if not input.MoveNext():
				return false
			if not char.IsWhiteSpace(input.Current):
				break
			mark = input.Mark()
		input.Reset(mark)
		return true

